package com.heima.search.listener;

import com.heima.common.contants.MQConstants;
import com.heima.search.Service.ArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 文章的ES同步
 */
@Component
@Slf4j
public class ArticleDocumentListener {
    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 上架->ES
     */
    @KafkaListener(topics = MQConstants.WM_NEW_UP_ES_TOPIC)
    public void handlerUpES(String value){
        log.info("开始导入数据到ES {}");
        if(StringUtils.isNotEmpty(value)){
            articleSearchService.saveToES(Long.valueOf(value));

        }
    }

    /**
     * 下架->ES
     */
    @KafkaListener(topics = MQConstants.WM_NEW_DOWN_ES_TOPIC)
    public void handlerDownES(String value){
        log.info("开始移除ES的数据 {}");
        if(StringUtils.isNotEmpty(value)){
            articleSearchService.removeFromES(Long.valueOf(value));

        }
    }
}
